package com.netbao.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.pro.mapper.WayBillMapper;
import com.netbao.pro.model.WayBill;
import com.netbao.pro.model.WayBillExample;
import com.netbao.pro.service.WayBillService;
import com.netbao.sys.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WayBillServiceImpl implements WayBillService {
    @Resource
    private WayBillMapper wayBillMapper;

    @Override
    public PageResult list(PageBean pageBean, String proName) {
        WayBillExample wayBillExample = new WayBillExample();
        WayBillExample.Criteria criteria = wayBillExample.createCriteria().
                andEidEqualTo(ShiroUtils.getUserEntity().getEid().longValue())
                .andCstatusNotEqualTo((long)2);
        if(!StringUtils.isEmpty(proName)){
            criteria.andPronameLike("%"+proName+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageHelper.orderBy("opstatus");
        PageInfo<WayBill> wayBillPageInfo = new PageInfo<>(wayBillMapper.selectByExample(wayBillExample));
        return new PageResult(wayBillPageInfo.getTotal(),wayBillPageInfo.getList());
    }

    @Override
    @Transactional
    public Result save(WayBill wayBill) {
        Date date = new Date();
        if(wayBill.getId()==null){
            Date stime = wayBill.getStime();
            Date etime = wayBill.getEtime();
            int min = (int) (( etime.getTime() - stime.getTime()) / (1000*60));
            if(min<20){
                return Result.error("运单时间太短，请检查开始时间和结束时间");
            }
            WayBillExample wayBillExample = new WayBillExample();
            wayBillExample.createCriteria().andIdcardEqualTo(wayBill.getIdcard());
            List<WayBill> wayBills = wayBillMapper.selectByExample(wayBillExample);
            for (WayBill bill : wayBills) {
                Date stime1 = bill.getStime();
                Date etime1 = bill.getEtime();
                if((stime.after(stime1)&&stime.before(etime1))||
                        (etime.after(stime1) &&etime.before(etime1)) ||
                        (stime.before(stime1)&&etime.after(etime1))
                ){
                    return Result.error("时间不合法，请输入正确时间");
                }
            }
            wayBill.setIssigned(true);
            wayBill.setChecktime(date);
            wayBill.setCstatus((long)1);
            wayBill.setEid(ShiroUtils.getUserEntity().getEid().longValue());
            wayBill.setOpid(ShiroUtils.getUserId().toString());
            wayBill.setOpstatus(true);
            wayBillMapper.insert(wayBill);//添加运单
            return Result.ok();
        }else {
            wayBill.setOpstatus(true);
            if(wayBill.getIssigned()!=null){
                if(wayBill.getIssigned()){
                    return Result.error("该条已签单，请勿重复操作！");
                }
                wayBill.setCstatus((long)1);
                wayBill.setChecktime(date);
            }
            wayBillMapper.updateByPrimaryKeySelective(wayBill);
        }
        return Result.ok();
    }

}
